<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:test="http://xsltut.googlecode.com"
	xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0">
	<xsl:include href="common.xsl"/>
	<xsl:output encoding="UTF-8" method="xml" indent="yes"/>
	<test:testCase name="schema" inputSchema="../schema/sqlzen.rng" outputSchema="../external/schema/relaxng.rng">
		<test:test name="empty">
			<test:input>
				<sqlzen sqlzenVersion="0.1" targetName="test"/>
			</test:input>
			<test:expect>
				<grammar xmlns="http://relaxng.org/ns/structure/1.0"
					xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
					xmlns:test="http://xsltut.googlecode.com" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
					<include href="file:///c:/works/sqlzen/schema/sqlzen-base.rng"/>
					<define name="dbTags">
						<empty/>
					</define>
					<define name="tableTags">
						<empty/>
					</define>
					<define name="wordCho">
						<empty/>
					</define>
					<define name="singleWordCho">
						<empty/>
					</define>
					<define name="entityPrefixCho">
						<empty/>
					</define>
					<define name="entitySuffixCho">
						<empty/>
					</define>
					<define name="attributePrefixCho">
						<empty/>
					</define>
					<define name="attributeSuffixCho">
						<empty/>
					</define>
					<define name="tableNameCho">
						<empty/>
					</define>
					<define name="tableColumnNameCho">
						<empty/>
					</define>
					<define name="columnNameCho">
						<empty/>
					</define>
					<define name="viewNameCho">
						<empty/>
					</define>
				</grammar>
			</test:expect>
		</test:test>
		<test:test name="dictionary">
			<test:input>
				<sqlzen sqlzenVersion="0.1" targetName="test">
					<rule>
						<affixes>
							<entitySuffix name="Mst"/>
							<attributeSuffix name="No"/>
							<attributeSuffix name="Seq"/>	
						</affixes>												
					</rule>
					<dictionary>
						<word name="Account">
							<comment lang="ko">계정</comment>
						</word>
						<word name="Character"/>
						<word name="Item"/>
						<word name="Get"/>
					</dictionary>
				</sqlzen>
			</test:input>
			<test:expect>
				<grammar xmlns="http://relaxng.org/ns/structure/1.0"
					xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
					xmlns:test="http://xsltut.googlecode.com" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
					<include href="file:///c:/works/sqlzen/schema/sqlzen-base.rng"/>
					<define name="dbTags">
						<empty/>
					</define>
					<define name="tableTags">
						<empty/>
					</define>
					<define name="wordCho">
						<choice>
							<value>Account</value>
							<a:documentation>계정</a:documentation>
							<value>Character</value>
							<value>Item</value>
							<value>Get</value>
						</choice>
					</define>
					<define name="singleWordCho">
						<choice>
							<value>Account</value>
							<a:documentation>계정</a:documentation>
							<value>Character</value>
							<value>Item</value>
							<value>Get</value>
						</choice>
					</define>
					<define name="entityPrefixCho">
						<empty/>
					</define>
					<define name="entitySuffixCho">
						<choice>
							<value>Mst</value>
						</choice>
					</define>
					<define name="attributePrefixCho">
						<empty/>
					</define>
					<define name="attributeSuffixCho">
						<choice>
							<value>No</value>
							<value>Seq</value>
						</choice>
					</define>
					<define name="tableNameCho">
						<empty/>
					</define>
					<define name="tableColumnNameCho">
						<empty/>
					</define>
					<define name="columnNameCho">
						<empty/>
					</define>
					<define name="viewNameCho">
						<empty/>
					</define>
				</grammar>
			</test:expect>
		</test:test>
		<test:test name="tableColumnName">
			<test:input>
				<sqlzen sqlzenVersion="0.1" targetName="test">
					<rule>
						<affixes>
							<entitySuffix name="Mst"/>
							<attributeSuffix name="No"/>
							<attributeSuffix name="Seq"/>
							<dbObjects>
								<table suffix="T"/>
							</dbObjects>
						</affixes>						
					</rule>
					<dictionary>
						<word name="Account"/>
						<word name="Character"/>
						<word name="Item"/>
						<word name="Get"/>
						<compositeWord>
							<wordRef name="Character"/>
							<wordRef name="Item"/>
						</compositeWord>						
						<tableName>
							<wordRef name="Account"/>
							<entitySuffixRef name="Mst"/>
						</tableName>
						<columnName>
							<wordRef name="Account"/>
							<attributeSuffixRef name="Nm"/>
						</columnName>
						<columnName>
							<wordRef name="Item"/>
							<attributeSuffixRef name="No"/>
						</columnName>
					</dictionary>
					<database>
						<table name="AccountMst">
							<columns>
								<column name="AccountNm" type="varChar" length="10"/>
								<column name="ItemNo" type="integer"/>
							</columns>
						</table>
					</database>
				</sqlzen>
			</test:input>
			<test:expect>
				<grammar xmlns="http://relaxng.org/ns/structure/1.0"
					xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
					xmlns:test="http://xsltut.googlecode.com" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
					<include href="file:///c:/works/sqlzen/schema/sqlzen-base.rng"/>
					<define name="dbTags">
						<empty/>
					</define>
					<define name="tableTags">
						<empty/>
					</define>
					<define name="wordCho">
						<choice>
							<value>Account</value>
							<value>Character</value>
							<value>Item</value>
							<value>Get</value>
							<value>CharacterItem</value>
						</choice>
					</define>
					<define name="singleWordCho">
						<choice>
							<value>Account</value>
							<value>Character</value>
							<value>Item</value>
							<value>Get</value>
						</choice>
					</define>
					<define name="entityPrefixCho">
						<empty/>
					</define>
					<define name="entitySuffixCho">
						<choice>
							<value>Mst</value>
						</choice>
					</define>
					<define name="attributePrefixCho">
						<empty/>
					</define>
					<define name="attributeSuffixCho">
						<choice>
							<value>No</value>
							<value>Seq</value>
						</choice>
					</define>
					<define name="tableNameCho">
						<choice>
							<value>AccountMstT</value>
						</choice>
					</define>
					<define name="tableColumnNameCho">
						<choice>
							<value>AccountMst.AccountNm</value>
							<value>AccountMst.ItemNo</value>
						</choice>
					</define>
					<define name="columnNameCho">
						<choice>
							<value>AccountNm</value>
							<value>ItemNo</value>
						</choice>
					</define>
					<define name="viewNameCho">
						<empty/>
					</define>
				</grammar>
			</test:expect>
		</test:test>
		<test:test name="tag">
			<test:input>
				<sqlzen sqlzenVersion="0.1" targetName="test">
					<rule>
						<tags>
							<dbTag name="locale" required="true">
								<comment lang="ko">지역구분</comment>
								<value name="KR">
									<comment lang="ko">한국</comment>
								</value>
								<value name="US">
									<comment lang="ko">미국</comment>
								</value>
							</dbTag>
							<dbTag name="bussiness">
								<comment lang="ko">사업구분</comment>
								<value name="PUB">
									<comment lang="ko">퍼블리슁</comment>
								</value>
								<value name="SEL">
									<comment lang="ko">자체서비스</comment>
								</value>
							</dbTag>
							<tableTag name="ownership">
								<comment lang="ko">테이블의 쓰기분류</comment>
								<value name="CON">
									<comment lang="ko">읽기전용</comment>
								</value>
								<value name="SVR">
									<comment lang="ko">서버별 정보</comment>
								</value>
							</tableTag>
						</tags>
					</rule>
				</sqlzen>
			</test:input>
			<test:expect>
				<grammar xmlns="http://relaxng.org/ns/structure/1.0"
					xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
					xmlns:test="http://xsltut.googlecode.com" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
					<include href="file:///c:/works/sqlzen/schema/sqlzen-base.rng"/>
					<define name="dbTags">
						<interleave>
							<ref name="dbTag-locale"/>
							<ref name="dbTag-bussiness"/>
						</interleave>
					</define>
					<define name="tableTags">
						<interleave>
							<ref name="tableTag-ownership"/>
						</interleave>
					</define>
					<define name="wordCho">
						<empty/>
					</define>
					<define name="singleWordCho">
						<empty/>
					</define>
					<define name="entityPrefixCho">
						<empty/>
					</define>
					<define name="entitySuffixCho">
						<empty/>
					</define>
					<define name="attributePrefixCho">
						<empty/>
					</define>
					<define name="attributeSuffixCho">
						<empty/>
					</define>
					<define name="tableNameCho">
						<empty/>
					</define>
					<define name="tableColumnNameCho">
						<empty/>
					</define>
					<define name="columnNameCho">
						<empty/>
					</define>
					<define name="viewNameCho">
						<empty/>
					</define>
					<define name="dbTag-locale">
						<attribute name="localeTag">
							<a:documentation>지역구분</a:documentation>
							<choice>
								<value>KR</value>
								<a:documentation>한국</a:documentation>
								<value>US</value>
								<a:documentation>미국</a:documentation>
							</choice>
						</attribute>
					</define>
					<define name="dbTag-bussiness">
						<optional>
							<attribute name="bussinessTag">
								<a:documentation>사업구분</a:documentation>
								<choice>
									<value>PUB</value>
									<a:documentation>퍼블리슁</a:documentation>
									<value>SEL</value>
									<a:documentation>자체서비스</a:documentation>
								</choice>
							</attribute>
						</optional>
					</define>
					<define name="tableTag-ownership">
						<optional>
							<attribute name="ownershipTag">
								<a:documentation>테이블의 쓰기분류</a:documentation>
								<choice>
									<value>CON</value>
									<a:documentation>읽기전용</a:documentation>
									<value>SVR</value>
									<a:documentation>서버별 정보</a:documentation>
								</choice>
							</attribute>
						</optional>
					</define>
				</grammar>
			</test:expect>
		</test:test>
	</test:testCase>
	<xsl:template match="/">
		<grammar xmlns="http://relaxng.org/ns/structure/1.0" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
			xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:test="http://xsltut.googlecode.com"
			datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
			<xsl:apply-templates/>
		</grammar>
	</xsl:template>
	<xsl:template match="sqlzen" xmlns="http://relaxng.org/ns/structure/1.0">
		<include>
			<xsl:attribute name="href">
				<xsl:value-of select="concat( $sqlzen-path, '/schema/sqlzen-base.rng' )"/>
			</xsl:attribute>
		</include>
		<define name="dbTags">
			<xsl:choose>
				<xsl:when test="rule/tags/dbTag">
					<interleave>
						<xsl:for-each select="rule/tags/dbTag">
							<ref>
								<xsl:attribute name="name">
									<xsl:value-of select="concat( 'dbTag-', @name )"/>
								</xsl:attribute>
							</ref>
						</xsl:for-each>
					</interleave>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="tableTags">
			<xsl:choose>
				<xsl:when test="rule/tags/tableTag">
					<interleave>
						<xsl:for-each select="rule/tags/tableTag">
							<ref>
								<xsl:attribute name="name">
									<xsl:value-of select="concat( 'tableTag-', @name )"/>
								</xsl:attribute>
							</ref>
						</xsl:for-each>
					</interleave>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="wordCho">
			<xsl:choose>
				<xsl:when test="dictionary/word">
					<choice>
						<xsl:for-each select="dictionary/word">
							<value>
								<xsl:value-of select="@name"/>
							</value>
							<xsl:call-template name="putCommentAnnotation"/>
						</xsl:for-each>
						<xsl:for-each select="dictionary/compositeWord">
							<value>
								<xsl:for-each select="wordRef">
									<xsl:value-of select="@name"/>
								</xsl:for-each>
							</value>
							<xsl:call-template name="putCommentAnnotation"/>
						</xsl:for-each>
					</choice>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="singleWordCho">
			<xsl:choose>
				<xsl:when test="dictionary/word">
					<choice>
						<xsl:for-each select="dictionary/word">
							<value>
								<xsl:value-of select="@name"/>
							</value>
							<xsl:call-template name="putCommentAnnotation"/>
						</xsl:for-each>
					</choice>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="entityPrefixCho">
			<xsl:choose>
				<xsl:when test="rule/affixes/entityPrefix">
					<choice>
						<xsl:for-each select="rule/affixes/entityPrefix">
							<value>
								<xsl:value-of select="@name"/>
							</value>
							<xsl:call-template name="putCommentAnnotation"/>
						</xsl:for-each>
					</choice>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="entitySuffixCho">
			<xsl:choose>
				<xsl:when test="rule/affixes/entitySuffix">
					<choice>
						<xsl:for-each select="rule/affixes/entitySuffix">
							<value>
								<xsl:value-of select="@name"/>
							</value>
							<xsl:call-template name="putCommentAnnotation"/>
						</xsl:for-each>
					</choice>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="attributePrefixCho">
			<xsl:choose>
				<xsl:when test="rule/affixes/attributePrefix">
					<choice>
						<xsl:for-each select="rule/affixes/attributePrefix">
							<value>
								<xsl:value-of select="@name"/>
							</value>
							<xsl:call-template name="putCommentAnnotation"/>
						</xsl:for-each>
					</choice>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="attributeSuffixCho">
			<xsl:choose>
				<xsl:when test="rule/affixes/attributeSuffix">
					<choice>
						<xsl:for-each select="rule/affixes/attributeSuffix">
							<value>
								<xsl:value-of select="@name"/>
							</value>
							<xsl:call-template name="putCommentAnnotation"/>
						</xsl:for-each>
					</choice>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="tableNameCho">
			<xsl:choose>
				<xsl:when test="dictionary/tableName">
					<choice>
						<xsl:for-each select="dictionary/tableName">
							<value>
								<xsl:value-of select="$tablePrefix"/>
								<xsl:call-template name="putDbObjectName"/>
								<xsl:value-of select="$tableSuffix"/>
							</value>
							<xsl:call-template name="putCommentAnnotation"/>
						</xsl:for-each>
					</choice>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="tableColumnNameCho">
			<xsl:choose>
				<xsl:when test="database/table/columns/column">
					<choice>
						<xsl:for-each select="database/table">
							<xsl:variable name="tableName" select="@name"/>
							<xsl:for-each select="columns/column">
								<value>
									<xsl:value-of select="concat( $tableName, '.', @name )"/>
								</value>
								<xsl:call-template name="putCommentAnnotation"/>
							</xsl:for-each>
						</xsl:for-each>
					</choice>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="columnNameCho">
			<xsl:choose>
				<xsl:when test="dictionary/columnName">
					<choice>
						<xsl:for-each select="dictionary/columnName">
							<value>
								<xsl:call-template name="putDbObjectName"/>
							</value>
							<xsl:call-template name="putCommentAnnotation"/>
						</xsl:for-each>
					</choice>
				</xsl:when>
				<xsl:otherwise>
					<empty/>
				</xsl:otherwise>
			</xsl:choose>
		</define>
		<define name="viewNameCho">
			<empty/>
		</define>
		<xsl:for-each select="rule/tags">
			<xsl:for-each select="dbTag">
				<define>
					<xsl:attribute name="name">
						<xsl:value-of select="concat( 'dbTag-', @name )"/>
					</xsl:attribute>
					<xsl:choose>
						<xsl:when test="@required = 'true'">
							<xsl:call-template name="putDbTagAttr"/>
						</xsl:when>
						<xsl:otherwise>
							<optional>
								<xsl:call-template name="putDbTagAttr"/>
							</optional>
						</xsl:otherwise>
					</xsl:choose>
				</define>
			</xsl:for-each>
			<xsl:for-each select="tableTag">
				<define>
					<xsl:attribute name="name">
						<xsl:value-of select="concat( 'tableTag-', @name )"/>
					</xsl:attribute>
					<xsl:choose>
						<xsl:when test="@required = 'true'">
							<xsl:call-template name="putTableTagAttr"/>
						</xsl:when>
						<xsl:otherwise>
							<optional>
								<xsl:call-template name="putTableTagAttr"/>
							</optional>
						</xsl:otherwise>
					</xsl:choose>
				</define>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="putTableTagAttr" xmlns="http://relaxng.org/ns/structure/1.0">
		<attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="concat( @name, 'Tag' )"/>
			</xsl:attribute>
			<xsl:call-template name="putCommentAnnotation"/>
			<choice>
				<xsl:for-each select="value">
					<value>
						<xsl:value-of select="@name"/>
					</value>
					<xsl:call-template name="putCommentAnnotation"/>
				</xsl:for-each>
			</choice>
		</attribute>
	</xsl:template>
	<xsl:template name="putDbTagAttr" xmlns="http://relaxng.org/ns/structure/1.0">
		<attribute>
			<xsl:attribute name="name">
				<xsl:value-of select="concat( @name, 'Tag' )"/>
			</xsl:attribute>
			<xsl:call-template name="putCommentAnnotation"/>
			<choice>
				<xsl:for-each select="value">
					<value>
						<xsl:value-of select="@name"/>
					</value>
					<xsl:call-template name="putCommentAnnotation"/>
				</xsl:for-each>
			</choice>
		</attribute>
	</xsl:template>
</xsl:stylesheet>
